﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class Administrator_Default : System.Web.UI.Page {
    private DataTable objDT;
    private string username, control = string.Empty;

    protected void Page_Load(object sender, EventArgs e) {
        try {
            if (Request["control"] != null) control = Request["control"].ToString();
            if (Session["ADMIN"] == null) {
                Response.Redirect("Login.aspx?control=" + control);
            } else {
                if (string.IsNullOrEmpty(control))
                    control = "ControlPanel";
                loadControls(control);
                if (!IsPostBack) {
                    WriteLog(control, Administrator.USER_ID, ClientIP, DateTime.Now);
                    ShowMainMenu();
                    ltrUserName.Text = Administrator.FULL_NAME;
                    username = Administrator.USER_NAME;
                }
            }
        } catch (Exception ex) {
            Javascripts.Alert(this.Page, "Phát hiện lỗi sau: " + ex.Message);
        }
    }

    protected string ClientIP {
        get {
            if (String.IsNullOrEmpty(HttpContext.Current.Request.UserHostAddress))
                return "0.0.0.0";
            else return HttpContext.Current.Request.UserHostAddress;
        }
    }

    protected void loadControls(string control) {
        plhdMain.Controls.Clear();
        UserControl controls = (UserControl)Page.LoadControl("../Controls/" + control + ".ascx");
        plhdMain.Controls.Add(controls);
    }

    protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        try {
            HtmlGenericControl spanIcon = (HtmlGenericControl)e.Item.FindControl("spanIcon");
            HiddenField hdfThumIcon = e.Item.FindControl("hdfThumIcon") as HiddenField;
            HiddenField hdfDescription = e.Item.FindControl("hdfDescription") as HiddenField;
            if (String.IsNullOrEmpty(hdfThumIcon.Value.Trim())) {
                spanIcon.Attributes.Add("class", "normal");
                spanIcon.InnerHtml = hdfDescription.Value;
                HtmlGenericControl spanName = (HtmlGenericControl)e.Item.FindControl("spanName");
                spanName.Visible = false;
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
    protected void rptChildMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        try {
            HiddenField hdfAdmin_module_id = (HiddenField)e.Item.FindControl("hdfAdmin_module_id");
            objDT = ShowMenuByParentID(Convert.ToInt32(hdfAdmin_module_id.Value));
            if (objDT != null && objDT.Rows.Count > 0) {
                HtmlAnchor aHasChild = (HtmlAnchor)e.Item.FindControl("aHasChild");
                aHasChild.Attributes.Add("class", "childs");
            } else {
                HtmlGenericControl ulChild2 = (HtmlGenericControl)e.Item.FindControl("ulChild2");
                ulChild2.Visible = false;
                Repeater rptChild2 = (Repeater)e.Item.FindControl("rptChild2");
                rptChild2.Visible = false;
            }
            if (e.Item.ItemIndex + 1 == objDT.Rows.Count) {
                HtmlGenericControl liChild1 = (HtmlGenericControl)e.Item.FindControl("liChild1");
                liChild1.Attributes.Add("class", "last");
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
    protected void rptChild2_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        try {
            objDT = ShowMenuByParentID(Convert.ToInt32(Eval("admin_module_id")));
            if (objDT != null && objDT.Rows.Count > 0) {
                if (e.Item.ItemIndex + 1 == objDT.Rows.Count) {
                    HtmlGenericControl liChild2 = (HtmlGenericControl)e.Item.FindControl("liChild2");
                    liChild2.Attributes.Add("class", "last");
                }
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
    protected void ShowMainMenu() {
        try {
            DataTable objDT = ShowMenuByParentID(0);
            if (objDT.Rows.Count > 0) {
                rptMenu.DataSource = objDT;
                rptMenu.DataBind();
            }
        } catch (Exception ex) {
            Javascripts.Alert(this.Page, "Phát hiện lỗi sau: " + ex.Message);
        }
    }
    protected DataTable ShowMenuByParentID(int parent_id) {
        try {
            DataTable objDTs = BO_cms_admin_modules.SelectParent(true, parent_id);
            return objDTs;
        } catch {
            return null;
        }
    }
    protected string Get_Menu_Link(string link) {
        if (link.Equals("#")) {
            return "#";
        } else {
            return "Default.aspx?control=" + link;
        }
    }

    int[] listAdminID = { 4, 10, 11, 12, 13, 14 };
    protected string CheckRole(int admin_module_id, string menu) {
        foreach (int id in listAdminID) {
            if (admin_module_id == id) {
                return menu + "---";
            }
        }
        return menu;
    }

    protected void WriteLog(string admin_module_id, int user_id, string ip, DateTime AccessDate) {
        try {
            BO_cms_user_access_log.Insert(admin_module_id, user_id, ip, AccessDate, true);
        } catch {
            return;
        }
    }
}